﻿#region

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ink_Master.Bots.InkMaster.Objects;
using Styx;

#endregion

namespace Ink_Master.Bots.InkMaster.InfoBuilders.Static
{
    public static class Herbs
    {
        /// <summary>
        ///   Get's the herbs required to make a pigment from the pigment.
        /// </summary>
        /// <param name = "pigmentname">The pigment name.</param>
        /// <returns></returns>
        public static ArrayList HerbsFromPigment(string pigmentname)
        {
            Dictionary<string, ArrayList> herbs = new Dictionary<string, ArrayList>();
            herbs.Add("Alabaster Pigment", new ArrayList {"Earthroot", "Peacebloom", "Silverleaf"});
            return herbs.Where(x => x.Key == pigmentname).Select(x => x.Value).FirstOrDefault();
        }

        public static Dictionary<string, ArrayList> Herbcontinents()
        {
            /*
            * 0 - Azeroth
            * 1 - Kalimdor
            * 2 - Eastern Kingdoms
            * 3 - Outland
            * 4 - Northrend
            * 5 - The Maelstrom
            */
            //TODO Herbs and continents
            Dictionary<string, ArrayList> herbsandcontinents = new Dictionary<string, ArrayList>();
            herbsandcontinents.Add("Earthroot", new ArrayList {1, 2});
            herbsandcontinents.Add("Peacebloom", new ArrayList {1, 2});
            herbsandcontinents.Add("Silverleaf", new ArrayList {1, 2});
            return herbsandcontinents;
        }

        public static uint GetNearestContinent(string herb)
        {
            // Get My Continent
            uint mycontinent = StyxWoW.Me.MapId;
            // Get the dictionary of herbs and continents 
            Dictionary<string, ArrayList> herbsandcontinets = Herbcontinents();
            // Get continents from herb dictionary
            List<uint> herbcontinets = new List<uint>();
            foreach (
                KeyValuePair<string, ArrayList> herbcontinent in
                    herbsandcontinets.Where(herbcontinent => herbcontinent.Key == herb))
            {
                herbcontinets.AddRange(herbcontinent.Value.Cast<uint>());
            }
            // OK we've got all the continents in herbcontinents list
            // If the list contains our continent return that
            return herbcontinets.Contains(mycontinent) ? mycontinent : herbcontinets[0];
            // Ok our continent isnt found return the first one from the list
        }

        public static string GetNearestHerb(InkCollector.InkCollectionInfo inkCollection)
        {
            uint myContinent = StyxWoW.Me.MapId;
            // Get herbs that we're looking at from the inkCollection
            List<string> herbsToLookFor = inkCollection.Herbs.Cast<string>().ToList();
            // Get the dictionary of herbs and continents 
            Dictionary<string, ArrayList> herbsandcontinets = Herbcontinents();
            // and delete stuff we don't want
            foreach (KeyValuePair<string, ArrayList> s in herbsandcontinets.Where(s => !herbsToLookFor.Contains(s.Key)))
            {
                herbsandcontinets.Remove(s.Key);
            }
            // OK so now in herbsandcotinents we now have a list of herbs we're interested in AND their continent ID's.
            // Lets find out if any herbs are on the same continent as us, if they are, lets just return the first one we find
            foreach (
                KeyValuePair<string, ArrayList> herb in
                    herbsandcontinets.Where(
                        herb => herb.Value.Cast<object>().Any(continent => (uint) continent == myContinent)))
            {
                return herb.Key;
            }
            return herbsToLookFor[0];
        }
    }
}